﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BookingEngine.cs" company="">
//   
// </copyright>
// <summary>
//   The engine for booking articles into the database.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Bonit_It.ViewModel.Cashier
{
    using System.Collections.Specialized;
    using System.Linq;

    using System.Collections.ObjectModel;
    using System.ComponentModel;

    using Bon.It.Proxy;

    using Bon_It.Model;
    using Bon_It.Model.TransferObjects;

    /// <summary>
    /// The engine for booking articles into the database.
    /// </summary>
    public class BookingEngine : INotifyPropertyChanged 
    {
        private ObservableCollection<BookingItem> list;

        private decimal total, payback,given;

        /// <summary>
        /// Gets or sets the total.
        /// </summary>
        /// <value>
        /// The total.
        /// </value>
        public decimal Total
        {
            get
            {
                return total;
            }
            set
            {
                total = value;
                this.OnPropertyChanged("Total");
            }
        }

        /// <summary>
        /// Gets or sets the given.
        /// </summary>
        /// <value>
        /// The given.
        /// </value>
        public decimal Given
        {
            get
            {
                return given;
            }
            set
            {

                given = value;
                // calculate
                decimal payBack = Given - Total;

                if (payBack >= 0)
                {
                    PayBack = payBack;
                }
                this.OnPropertyChanged("Given");
            }
        }


        /// <summary>
        /// Gets or sets the pay back.
        /// </summary>
        /// <value>
        /// The pay back.
        /// </value>
        public decimal PayBack
        {
            get
            {
                return payback;
            }
            set
            {
                payback= value;
                this.OnPropertyChanged("PayBack");
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BookingEngine"/> class.
        /// </summary>
        public BookingEngine()
        {
            List= new ObservableCollection<BookingItem>();
            List.CollectionChanged += this.ListCollectionChanged;
        }


        void ListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // Recalculate the total Value on change
            if (e.Action== NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Remove)
            {
                Total = list.Sum(i => i.Total);
            }
        }

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        /// <value>
        /// The list.
        /// </value>
        /// <summary>
        /// The buyed articles.
        /// </summary>
        public ObservableCollection<BookingItem> List
        {
            get
            {
                return list;
            }
            set
            {
                list = value;
                this.OnPropertyChanged("List");        
            }
        }

        /// <summary>
        /// Debits the specified input type.
        /// </summary>
        /// <param name="inputType">Type of the input.</param>
        public void Debit(string inputType)
        {
            DebitInformation.DebitTypes type= DebitInformation.DebitTypes.Cash;
            if (DebitInformation.DebitTypes.TryParse(inputType, out type))
            {
                var info = new DebitInformation();
                info.DebitType = type;
                ServiceProxy.Instance.Debit(List.ToArray(),info);

                List.Clear();
            }
        }

        /// <summary>
        /// Removes the last <see cref="Artikel"/>.
        /// </summary>
        public void RemoveLastArticle()
        {
            List.RemoveAt(List.Count-1);
        }

        /// <summary>
        /// Set Booking Items to the internal list.
        /// </summary>
        /// <param name="item">the <see cref="BookingItem"/></param>
        public void AddArticle(BookingItem item)
        {
            List.Add(item);
        }


        /// <summary>
        /// Store the articles to the rechnung table. 
        /// To complete the cashier.
        /// </summary>
        public void Bookingarticle()
        {
            
        }

        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="property">The property.</param>
        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        /// <summary>
        /// Tritt ein, wenn sich ein Eigenschaftswert ändert.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
